package com.example.neteasemusic.welcome

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.example.lib_base.manager.AdvManager
import com.example.lib_base.manager.UserMangaer
import com.example.lib_login.LoginActivity
import com.example.neteasemusic.MainActivity
import com.example.neteasemusic.databinding.FragmentGuideBinding
import java.util.Timer
import java.util.TimerTask


class GuideFragment : Fragment() {
    private lateinit var binding:FragmentGuideBinding
    private var time = 6
    private val timer = Timer()



    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        requireActivity().window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding = FragmentGuideBinding.inflate(inflater,container,false)
        return  binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.skip.text = "跳转$time"
        binding.uri = AdvManager.getInstance().getAdv()?.uri


        binding.skip.setOnClickListener {
            //检查是否登录
            if(checkLogin()) {
                //有
                requireActivity().startActivity(
                    Intent(
                        requireActivity(),
                        MainActivity::class.java
                    )
                )

            }else{
                //没有
                requireActivity().startActivity(
                    Intent(
                        requireActivity(),
                        LoginActivity::class.java
                    )
                )
            }
            requireActivity().finish()
        }

        //开启定时器
        timer.schedule(object :TimerTask(){
            override fun run() {


                requireActivity().runOnUiThread {
                    if(time == 0){
                        //检查是否登录
                        if(checkLogin()) {

                            //有
                            requireActivity().startActivity(
                                Intent(
                                    requireActivity(),
                                    MainActivity::class.java
                                )
                            )

                        }else{
                            //没有
                            requireActivity().startActivity(
                                Intent(
                                    requireActivity(),
                                    LoginActivity::class.java
                                )
                            )

                        }
                            requireActivity().finish()
                    }
                    binding.skip.text = "跳转$time"
                }
                time--
            }
        },0,1000)


    }

    fun checkLogin():Boolean{
        return UserMangaer.getInstance().isLogin()
    }


    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    companion object{
        @JvmStatic
        @BindingAdapter("imageUrl")  //必须使用静态方法
        fun bindImageUrl(view: ImageView, url:String){
            Glide.with(view).load(url).into(view)
        }
    }


}